package sg.edu.nus.iss.ooad.jass.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.IJobAdvertisementDAO;
import sg.edu.nus.iss.ooad.jass.dao.pattern.AbstractJASSDAOFactory;
import sg.edu.nus.iss.ooad.jass.entity.JobAdvertisement;

/**
 *
 * JobAdvertisement Controller. This class will handle all request related to the JobAdvertisement object.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : JobAdvertisementController.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class JobAdvertisementController {

        private final static Logger logger = Logger.getLogger(JobAdvertisementController.class.getName());
        private JobAdvertisement jobAdvertisement = null;
        private boolean editable = false;

        /**
         *
         */
        public JobAdvertisementController() {
        }

        /**
         *
         * @return
         */
        public JobAdvertisement getJobAdvertisement() {
                return jobAdvertisement;
        }

        /**
         *
         * @param jobAdvertisement
         */
        public void setJobAdvertisement(JobAdvertisement jobAdvertisement) {
                this.jobAdvertisement = jobAdvertisement;
        }

        /**
         * 
         * @return
         */
        public boolean isEditable() {
                return editable;
        }

        /**
         * 
         * @param editable
         */
        public void setEditable(boolean editable) {
                this.editable = editable;
        }

        /**
         *
         * @param jobadvertisement
         * @return
         * @throws java.lang.Exception
         */
        public boolean createJobAdvertisement(JobAdvertisement jobadvertisement) throws Exception {
                IJobAdvertisementDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.createJobAdvertisement(jobadvertisement);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param id
         * @return
         * @throws java.lang.Exception
         */
        public JobAdvertisement retrieveJobAdvertisement(String id) throws Exception {
                IJobAdvertisementDAO dao = null;
                JobAdvertisement retValue = null;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.retrieveJobAdvertisement(id);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param jobadvertisement
         * @return
         * @throws java.lang.Exception
         */
        public boolean updateJobAdvertisement(JobAdvertisement jobadvertisement) throws Exception {
                IJobAdvertisementDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.updateJobAdvertisement(jobadvertisement);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param jobadvertisement
         * @return
         * @throws java.lang.Exception
         */
        public boolean deleteJobAdvertisement(JobAdvertisement jobadvertisement) throws Exception {
                IJobAdvertisementDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.deleteJobAdvertisement(jobadvertisement);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * 
         * @return
         * @throws Exception
         */
        public List<JobAdvertisement> getAllJobAdvertisement() throws Exception {

                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.getAllJobAdvertisement();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
                return retValue;

        }

        /**
         * 
         * @param title
         * @return
         * @throws Exception
         */
        public List<JobAdvertisement> findJobAdvertisementByTitle(String advertiserID, String title) throws Exception {

                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.findJobAdvertisementByTitle(advertiserID, title);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
                return retValue;

        }

        /**
         *
         * @param location
         * @return
         * @throws Exception
         */
        public List<JobAdvertisement> findJobAdvertisementByLocation(String advertiserID, String location) throws Exception {

                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.findJobAdvertisementByLocation(advertiserID, location);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;

        }

        /**
         * Find JobAdvertisement by using JobAdvertisement Technical/Management Skills as search criteria
         * @param skills JobAdvertisement Technical/Management Skills.
         * @return List of JobAdvertisement.
         */
        public List<JobAdvertisement> findJobAdvertisementBySkills(String advertiserID, String skills) throws Exception {
                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.findJobAdvertisementBySkills(advertiserID, skills);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param skills
         * @return
         */
        public List<JobAdvertisement> findJobAdvertisementByAdvertisementDate(String advertiserID, int nDaysBefore) throws Exception {
                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.findJobAdvertisementByAdvertisementDate(advertiserID, nDaysBefore);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * 
         * @param salary
         * @return
         */
        public List<JobAdvertisement> findJobAdvertisementBySalary(String advertiserID, float salary) throws Exception {
                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.findJobAdvertisementBySalary(advertiserID, salary);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * Find JobAdvertisement from persistent data by using Job Advertisement Company ID as search criteria.
         * @param companyID Company ID
         * @return List of JobAdvertisement
         */
        public List<JobAdvertisement> findJobAdvertisementByCompanyId(String advertiserID, String companyID) throws Exception {
                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.findJobAdvertisementByCompanyId(advertiserID, companyID);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * Find JobAdvertisement from persistent data by using Job Advertisement Company Name as search criteria.
         * @param companyName Company Name
         * @return List of JobAdvertisement
         */
        public List<JobAdvertisement> findJobAdvertisementByCompanyName(String advertiserID, String companyName) throws Exception {
               IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = dao.findJobAdvertisementByCompanyName(advertiserID, companyName);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         *
         * @param advertiserID
         * @return
         * @throws Exception
         */
        public List<JobAdvertisement> findJobAdvertisementByAdvertiserID(String advertiserID) throws Exception {

                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = (ArrayList<JobAdvertisement>) dao.findJobAdvertisementByAdvertiserID(advertiserID);

                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
                return retValue;

        }

       /**
         *
         * @param advertiserName
         * @return
         * @throws Exception
         */
        public List<JobAdvertisement> findJobAdvertisementByAdvertiserName(String advertiserName) throws Exception {

                IJobAdvertisementDAO dao = null;
                List<JobAdvertisement> retValue = new ArrayList<JobAdvertisement>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                        retValue = (ArrayList<JobAdvertisement>) dao.findJobAdvertisementByAdvertiserName(advertiserName);

                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
                return retValue;

        }

        /**
         * Load JobAdvertisement object from persistent and cache it.
         * @throws Exception
         */
        public synchronized static void cache() throws Exception {
                IJobAdvertisementDAO dao = null;
                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getJobAdvertisementDAO();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
        }

}
